<?php

namespace addons\mental\model;

use think\Model;

class TestMainStatistics extends Model
{

    // 表名
    protected $name = 'mental_testmain_statistics';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    /**
     * 统计数据
     * $mainId      量表id
     * $type        类型:1=个人测试数,2=企业测试数,3=点赞数,4=测试提交数,5=拉起付款数,6=付款成功数
     * $extensionId 分销id
     */
    public static function setIncNum($mainId, $type, $extensionId = 0)
    {
        $data = self::where(['main_id' => $mainId, 'type' => $type])->whereTime('createtime', 'today')->find();
        if ($data) {
            $data->setInc('num');
        } else {
            self::create([
                'main_id' => $mainId,
                'type' => $type,
                'num' => 1,
            ]);
        }

        if ($type == 1) {
            ExtensionStatistics::setIncNum(1, $extensionId);
        } elseif ($type == 4) {
            ExtensionStatistics::setIncNum(2, $extensionId);
        } elseif ($type == 5) {
            ExtensionStatistics::setIncNum(3, $extensionId);
        } elseif ($type == 6) {
            ExtensionStatistics::setIncNum(4, $extensionId);
        }
        return true;
    }
}
